﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using fps_arrow.Properties;

namespace fps_arrow
{
    public partial class Arrow : Form
    {

        public Arrow()
        {
            InitializeComponent();
            this.AdjustWinPosToCenter();
        }
        private void AdjustWinPosToCenter()
        {
            // 屏幕中心
            int x = int.Parse(Properties.Settings.Default.OffsetX);
            int y = int.Parse(Properties.Settings.Default.OffsetY);

            Rectangle screenArea = System.Windows.Forms.Screen.GetBounds(this);
            this.Location = new System.Drawing.Point((screenArea.Width - this.Width) / 2 + x, (screenArea.Height - this.Height) / 2 + y);
        }

        private void Arrow_Load(object sender, EventArgs e)
        {
            // 在任务栏不显示      
            double transparency = int.Parse(Settings.Default.TC) / 100.0;
            this.ShowInTaskbar = false;
            this.SetWinTop();
            this.SetWindowMouseThrough();
            this.SetWindowTransparent();
            this.Opacity = transparency;
        }

        private void SetWindowTransparent()
        {
            // 图片的背景色和窗口的透明色一致
            Color color = Color.Blue;
            this.pictureBox.BackColor = color;
            this.TransparencyKey = color;

            // 图片的大小和窗口大小一致
            this.pictureBox.Width = this.Width;
            this.pictureBox.Height = this.Height;
            this.pictureBox.Location = new Point(0, 0);
        }

        // 注意调用时机
        private void DrawArrow(Graphics graphics)
        {
            int arrowLen = int.Parse(Settings.Default.LineLen);
            int lineWidth = int.Parse(Settings.Default.LineWidth);
            Pen pen = new Pen(Color.Red, lineWidth);
            // 4条线

            int arrowCenterSpace = int.Parse(Settings.Default.ArrowSpace);// 准星中心空隙

            int left = this.Width / 2 - arrowCenterSpace;
            graphics.DrawLine(pen, new Point(left, this.Height / 2), new Point(left - arrowLen, this.Height / 2));

            int right = this.Width / 2 + arrowCenterSpace;
            graphics.DrawLine(pen, new Point(right, this.Height / 2), new Point(right + arrowLen, this.Height / 2));
            
            int top = this.Height / 2 - arrowCenterSpace;
            graphics.DrawLine(pen, new Point(this.Width / 2, top), new Point(this.Width / 2, top - arrowLen));

            int bottom = this.Height / 2 + arrowCenterSpace;
            graphics.DrawLine(pen, new Point(this.Width / 2, bottom), new Point(this.Width / 2, bottom + arrowLen));

 
        }
        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

    
        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

        private const uint WS_EX_LAYERED = 0x80000;
        private const int GWL_EXSTYLE = -20; // 设置一个新的扩展风格
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int LWA_ALPHA = 0x2;

        // 设置窗体可以被鼠标穿透
        private void SetWindowMouseThrough()
        {
            GetWindowLong(this.Handle, GWL_EXSTYLE);
            SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
        }
        private void SetWinTop()
        {
            SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2);
        }

        private void OnTimer(object sender, EventArgs e)
        {
            
        }

        private void OnWindowPaint(object sender, PaintEventArgs e)
        {
            this.DrawArrow(e.Graphics);
        }
    }
}
